package com.djz.stuinfomge.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.djz.stuinfomge.entity.PageBean;
import com.djz.stuinfomge.entity.Speciality;
import com.djz.stuinfomge.exception.PageNumOutOfRange;
import com.djz.stuinfomge.service.SpecialityService;
import com.djz.stuinfomge.util.List2JSONUtil;
import com.djz.stuinfomge.util.WriteStrToRespUtil;
import com.opensymphony.xwork2.ActionContext;

public class SpecialityAct extends BaseAct {
	public SpecialityAct() {
		namespace = ActionContext.getContext().getActionInvocation().getProxy()
				.getNamespace();
		logger.debug("------------------------------namespac5e: " + namespace);
	}
	public String topLevelCategory(){
		list = service.topLevelCategory();
		return null;
	}
	public String detail(){
		logger.debug("------------------detail()");
		bean = service.get(id);
		return "detail";
	}
	
	
	public String remove() {
		logger.debug("------------------remove()");
		//利用约束异常，来判断是否可以删除
		service.remove(id);
		return "showResult";
		// return SUCCESS;
	}

	public String update() {
		logger.debug("方法名："
				+ Thread.currentThread().getStackTrace()[1].getMethodName());

		service.update(bean);
		
		//修改成功后，如果是“修改个人基本信息页面”，修改后，返回当前页面；
//		if(("person_basic_info").equals(function)){
//			return "currPage";
//		}
		
		return "showResult";

	}

	public String add() {
		logger.debug("方法名："
				+ Thread.currentThread().getStackTrace()[1].getMethodName());

		// if(bean.getSchoolSpeciality() == null)
		// logger.debug("---------------------------------------null");
		// else
		// logger.debug("---------------------------------------not null");
		
		//bean.setCreatetime(new Date());
		service.add(bean);
		return "showResult";
	}

	public String input() {
		logger.debug("方法名："
				+ Thread.currentThread().getStackTrace()[1].getMethodName());
		// 如果是编辑操作，则返回bean
		if (id != null)
			bean = service.get(id);
		return "input";
	}

//	public String getSpecialityByName() {
//		logger.debug("------------参数name：" + name);
//		logger.debug("-----------进入方法getSpecialityByName");
//		List list = service.getSpecialityByName(name);
//		logger.debug("客户列表list: " + list);
//		String json = List2JSONUtil.process(list);
//
//		HttpServletResponse resp = ServletActionContext.getResponse();
//		resp.setCharacterEncoding("utf-8");
//		WriteStrToRespUtil.process(resp, json);
//		return null;
//	}

	public String list() {

		logger.debug("方法名："
				+ Thread.currentThread().getStackTrace()[1].getMethodName());
		
		if (id == null)
			id = 1;
		try {
			// 使用bean储存查询条件
			page = service.getPage(id, size, bean);
			
		} catch (PageNumOutOfRange e) {
			// 不建议将逻辑写入catch中
			page = new PageBean<Speciality>();
			logger.debug("页号超出范围！");
		}
		logger.debug("--------------------bean: " + bean);
		
		return "list";
	}

	public Speciality getBean() {
		return bean;
	}

	public void setBean(Speciality bean) {
		this.bean = bean;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public PageBean<Speciality> getPage() {
		return page;
	}

	public void setPage(PageBean<Speciality> page) {
		this.page = page;
	}

	// 用于分页查询，总页数
	public long getPageCount() {
		return page.getTotalCount() / size + 1;
	}

	public String getExceptionName() {
		return exceptionName;
	}

	public void setExceptionName(String exceptionName) {
		this.exceptionName = exceptionName;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getLastPage() {
		return lastPage;
	}

	public void setLastPage(String lastPage) {
		this.lastPage = lastPage;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	


//	public String getFunction() {
//		return function;
//	}
//
//	public void setFunction(String function) {
//		this.function = function;
//	}


public List<Speciality> getList() {
		return list;
	}
	public void setList(List<Speciality> list) {
		this.list = list;
	}



	//相当于模块下的功能
	//private String function;
	private String name;
	private String lastPage;

	// 用于结果页判断
	private String actionName = ActionContext.getContext()
			.getActionInvocation().getProxy().getActionName();
	// 用于结果页判断
	private String exceptionName;

	// / 用于分页查询，单页显示的数据量
	private Integer size = 10;
	private PageBean<Speciality> page;
	@Autowired
	private SpecialityService service;
	private List<Speciality> list;
	private Speciality bean;

}
